package logic

import (
	"context"
	"selection-inventory/application/applet/internal/model"

	"selection-inventory/application/applet/internal/svc"
	"selection-inventory/application/applet/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateCommodityLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUpdateCommodityLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateCommodityLogic {
	return &UpdateCommodityLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UpdateCommodityLogic) UpdateCommodity(req *types.UpdateRequest) (resp bool, err error) {
	res := l.svcCtx.DB.Save(&model.Commodity{
		Id:      req.Id,
		Title:   req.Title,
		Picture: req.Picture,
		Ali:     req.Ali,
		Douyin:  req.Douyin,
		Pdd:     req.Pdd,
		Taobao:  req.Taobao,
	})
	if res.Error != nil {
		return false, err
	}
	return res.RowsAffected > 0, nil
}
